/*
 * 
 *    restaurant table
 *       id	
	 name
	 city
	 website
	 telephone
	 wifi
	 parking
 */
package hu.home.leva.db.restaurants;

import com.mysql.jdbc.Statement;
import hu.home.leva.db.DataBase;
import hu.home.leva.db.Entity;
import hu.home.leva.db.IManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.*;

/**
 *
 * @author leva
 */
public class RestaurantManager implements IManager {
    
    private DataBase database = DataBase.getInstance();

    @Override
    public List<Entity> getAll() {
        List<Entity> entities = new ArrayList<Entity>();
        try {
            Statement statement = (Statement) database.getConnection().createStatement();  //Nem tudom miért nem jó simán, konverzió nélkül
            ResultSet resultset;
            String selectRestaurants ="select * from restaurant;";
            resultset = statement.executeQuery(selectRestaurants);
            while(resultset.next()){
                int id = resultset.getInt("id");
                String name= resultset.getString("name");
                String city= resultset.getString("city");
                String website= resultset.getString("website");
                String telephone= resultset.getString("telephone");
                boolean wifi=resultset.getBoolean("wifi");
                boolean parking=resultset.getBoolean("parking");
                Restaurant restaurant = new Restaurant(id,name,city,website,telephone,wifi,parking);
                entities.add(restaurant);  
            }    
        } catch (SQLException ex) {
            Logger.getLogger(RestaurantManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return entities; 
    }

    @Override
    public Entity getById(int byid) {
        Restaurant restaurant=null;
        try{
            String selectById ="select * from user where user.id = ?;";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(selectById);
            preparedstatement.setInt(1, byid);
            ResultSet resultset = preparedstatement.executeQuery();
            while(resultset.next()){
                int id = resultset.getInt("id");
                String name= resultset.getString("name");
                String city= resultset.getString("city");
                String website= resultset.getString("website");
                String telephone= resultset.getString("telephone");
                boolean wifi=resultset.getBoolean("wifi");
                boolean parking=resultset.getBoolean("parking");
                restaurant = new Restaurant(id,name,city,website,telephone,wifi,parking);
            }
        }
        catch(Exception ex){
            Logger.getLogger(RestaurantManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return restaurant;
    }

    @Override
    public void saveAll(List<Entity> entities) {
        List<Restaurant> restaurants = new ArrayList<Restaurant>();
        restaurants = (List<Restaurant>) (Restaurant) entities;
        for(Restaurant restaurant:restaurants){
            save(restaurant);
        }
    }
    
    private void insert(Entity entity){
        Restaurant restaurant =(Restaurant) entity;
        try{
            String insertUser = "insert into restaurant(name,city,website,telephone,wifi,parking) values (?,?,?,?,?,?);";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(insertUser);
            preparedstatement.setString(1, restaurant.getName());
            preparedstatement.setString(2, restaurant.getCity());
            preparedstatement.setString(3, restaurant.getWebsite());
            preparedstatement.setString(4, restaurant.getTelephone());
            preparedstatement.setBoolean(5, restaurant.isWifi());
            preparedstatement.setBoolean(6, restaurant.isParking());
            preparedstatement.executeUpdate();
        }
        catch(Exception ex){
            Logger.getLogger(RestaurantManager.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
    
    private void update(Entity entity){
        Restaurant restaurant =(Restaurant) entity;
        try{
            String updateUser = "update restaurant set name=?,city=?,website=?,telephone=?,wifi=?,parking=? where id= "+entity.getId()+";";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(updateUser);
            preparedstatement.setString(1, restaurant.getName());
            preparedstatement.setString(2, restaurant.getCity());
            preparedstatement.setString(3, restaurant.getWebsite());
            preparedstatement.setString(4, restaurant.getTelephone());
            preparedstatement.setBoolean(5, restaurant.isWifi());
            preparedstatement.setBoolean(6, restaurant.isParking());
            preparedstatement.executeUpdate();
        }
        catch(Exception ex){
            Logger.getLogger(RestaurantManager.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }

    @Override
    public void save(Entity entity) {
         if(entity.getId()==-1){
            insert(entity);
        }
        else{
            update(entity);
        }
    }

    @Override
    public void delete(Entity entity) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteAll(List<Entity> entities) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
